package kr.hkit.powerjava.chapter14.submit; //그냥 연습중2

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.HeadlessException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

class NorthPanel extends JPanel { // 라벨과 텍스트필드 패널

	private JLabel lbl;
	JTextField jtf01;

	public NorthPanel() {

		setLayout(new FlowLayout());
		lbl = new JLabel("거리를 마일 단위로 입력하시오."); // 레이블
		jtf01 = new JTextField(10); // 텍스트필드
		add(lbl);
		add(jtf01);
	}

	public String getJtf01() {
		return jtf01.getText();
	}

	public void setJtf01(String text) {
		this.jtf01.setText(text);
	}

}

class CenterBtn extends JPanel { // 버튼 패널

	private JButton btn01;

	public CenterBtn() {
		btn01 = new JButton("변환");
		add(btn01);
	}
}

class SouthPanel extends JPanel { // 결과 텍스트필드 패널

	JTextField jtf02;

	public SouthPanel() {
		jtf02 = new JTextField(20);
		add(jtf02);
	}

	public String getJtf02() {
		return jtf02.getText();
	}

	public void setJtf02(String text) {
		jtf02.setText(text);
	}
}

class Mile2 extends JFrame {

	NorthPanel north;
	JButton btn01;
	SouthPanel south;

	public Mile2(String title) throws HeadlessException {
		super(title);
		init();
	}

	private void init() {

		setSize(400, 200);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		setLayout(new BorderLayout());

		north = new NorthPanel(); // 패널
		add(north, BorderLayout.NORTH);

		btn01 = new JButton("변환");
		btn01.addActionListener(new BtnListener());
		add(btn01, BorderLayout.CENTER);

		south = new SouthPanel(); // 패널
		add(south, BorderLayout.SOUTH);

		pack();
		setVisible(true);

	}

	private class BtnListener implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent e) {
			int res = 0;
			if (e.getActionCommand().equals("변환")) {
				try {
					res = Integer.parseInt(north.getJtf01().trim());
					south.setJtf02(res + " 마일은 " + res * 1.6 + "킬로미터입니다.");
				} catch (NumberFormatException err) {
					JOptionPane.showMessageDialog(null, "숫자만 입력하시오");
					north.setJtf01(" ");
				}
			}
		}
	}
}

public class MileToKiloCode {
	public static void main(String[] args) {
		new Mile2("마일을 킬로미터로 변환");
	}
}
